package com.briup.smartlabs.service.impl;

import java.util.*;

import com.briup.smartlabs.bean.ex.Device;
import com.briup.smartlabs.common.exceptions.DataValidateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.smartlabs.bean.SmartLabs;
import com.briup.smartlabs.mapper.SmartLabsMapper;
import com.briup.smartlabs.mapper.ex.DeviceMapper;
import com.briup.smartlabs.mapper.ex.SmartLabsExMapper;
import com.briup.smartlabs.service.LabsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class LabsServiceImpl implements LabsService {
	@Autowired
	private SmartLabsMapper labsMapper;
	
	@Autowired
	private SmartLabsExMapper labsExMapper;
	
	@Autowired
	private DeviceMapper deviceMapper;

	/**
	 * 更新
	 * @param labs
	 */
	@Override
	public void saveOrUpdate(SmartLabs labs) {
		//System.out.println("in saveOrUpdate ...");
		// 参数校验
		if (labs == null){
			throw new DataValidateException("参数为空!");
		}

		//判断实验室标识是否存在
		SmartLabs byLabsNum = labsExMapper.findByLabsNum(labs.getLabsNum(), labs.getId());
		if (byLabsNum != null) {
			throw new DataValidateException("实验室标识存在");
		}
		//判断网络地址是否被占用
		SmartLabs byNetAdres = labsExMapper.findByNetAdres(labs.getNetAdres(), labs.getId());
		if (byNetAdres != null) {
			throw new DataValidateException("网络地址存在");
		}
		//save or update
		// 判断是新增还是更新，根据id是否存在(指的是在sql表中存在)来判断更新还是新增
		// 查询id在sql中是否存在

		/**
		 *  或者将表修改为主键自增
		 */
//		SmartLabs smartLabs = labsMapper.selectByPrimaryKey(labs.getId());
		if (!Objects.equals(labs.getId(), "")){
			// 更新
			//labsMapper.updateByPrimaryKey(labs);
			labsExMapper.updateSelective(labs);
		}else {
			// 新增
			labs.setId(UUID.randomUUID().toString());
			labsMapper.insert(labs);
		}
	}

	@Override
	public Map<String, List<String>> removeByBatch(List<String> ids) {
		/**
		 *  返回结果：
		 *  	success： 在数据库中存在，并且成功删除的
		 *  	error:	 在数据库中不存在的，
		 * 业务：
		 * 	存在在数据库中的实验室，判断其是否有关联的设备，如果解除有关联。
		 */
		// 1. 创建两个集合用来存储在数据库中存在的和不存在的数据
		List<String> existIds = new ArrayList<>();
		List<String> noExistIds = new ArrayList<>();
		// 2.首先查找ids在数据库中存不存在
		List<SmartLabs> smartLabs = labsExMapper.selectAll(ids);
		List<String> list = new ArrayList<>();
		if(!smartLabs.isEmpty()) {
			for (SmartLabs smartLab : smartLabs) {
				list.add(smartLab.getId());
			}
		}
		for (String id : ids) {
			if(list.contains(id)){
				existIds.add(id);
			}else{
				noExistIds.add(id);
			}
		}
		/*for (String id : ids) {
			SmartLabs smartLabs = labsMapper.selectByPrimaryKey(id);
			if (smartLabs != null) {
				existIds.add(id);
			}else{
				noExistIds.add(id);
			}
		}*/
		// 3.将成功的数据和失败的数据存储到map集合中
		Map<String , List<String>> map = new HashMap<>();
		map.put("success", existIds);
		map.put("error", noExistIds);
		if (!existIds.isEmpty()) {
			// 4.删除之前解除带关联的设备
			deviceMapper.unbindDeviceByLabsId(existIds);
			// 5.删除查询到的存在的数据
			labsExMapper.deleteByBatch(existIds);
		}
		return map;

	}

	@Override
	public PageInfo<SmartLabs> findByCondition(int pageNum, int pageSize, String key) {
		PageHelper.startPage(pageNum, pageSize);
		key = key != null ? "%" + key + "%" : null;
		List<SmartLabs> smartLabs = labsExMapper.selectByCondition(key);
		return new PageInfo<>(smartLabs);
	}

	@Override
	public void removeById(String id) {
		if (id == null && "".equals(id)) {
			throw new DataValidateException("删除失败，参数为空!");
		}
		SmartLabs smartLabs = labsMapper.selectByPrimaryKey(id);
		if (smartLabs == null) {
			throw new DataValidateException("删除失败，实验室不存在!");
		}
		Device device = deviceMapper.selectById(id);
		if (device == null) {
			throw new DataValidateException("这个实验室下边还有设备，请删除实验室下边的设备在删除实验室");
		}
		labsMapper.deleteByPrimaryKey(id);
	}

}




